package com.gewu.pm.ui.adapter.order;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.CategoryTypeBean;

/**
 * 商品分类适配器
 */
public final class CategoryTypeAdapter extends AppAdapter<CategoryTypeBean> {

    private OnCategoryClickListener mListener;

    public CategoryTypeAdapter(Context context) {
        super(context);
    }

    public void setOnCategoryClickListener(OnCategoryClickListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).isChild() ? 1 : 0;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CategoryViewHolder();
    }

    private final class CategoryViewHolder extends AppAdapter<?>.ViewHolder {

        private final RelativeLayout mItemLayout;
        private final View mParentBg;
        private final View mChildBg;
        private final View mSubIndicator;
        private final TextView mNameView;
        private final ImageView mArrowView;

        private CategoryViewHolder() {
            super(R.layout.category_type_item);
            mItemLayout = findViewById(R.id.rl_category_item);
            mParentBg = findViewById(R.id.view_parent_bg);
            mChildBg = findViewById(R.id.view_child_bg);
            mSubIndicator = findViewById(R.id.view_sub_indicator);
            mNameView = findViewById(R.id.tv_category_name);
            mArrowView = findViewById(R.id.iv_arrow);

            mItemLayout.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onCategoryClick(getViewHolderPosition());
                }
            });
        }

        @Override
        public void onBindView(int position) {
            CategoryTypeBean category = getItem(position);
            
            mNameView.setText(category.getName());

            // 如果是子分类
            if (category.isChild()) {
                // 显示白色背景
                mParentBg.setVisibility(View.GONE);
                mChildBg.setVisibility(View.VISIBLE);
                
                // 选中时显示红色竖线
                mSubIndicator.setVisibility(category.isSelected() ? View.VISIBLE : View.GONE);
                mArrowView.setVisibility(View.GONE);
                mNameView.setTextColor(ContextCompat.getColor(getContext(), 
                    category.isSelected() ? R.color.red_app : R.color.cb6));
            } else {
                // 父分类
                mChildBg.setVisibility(View.GONE);
                mSubIndicator.setVisibility(View.GONE);
                
                // 选中时显示红色背景
                mParentBg.setVisibility(category.isSelected() ? View.VISIBLE : View.GONE);
                
                // 有子分类显示箭头
                if (category.isHasChildren()) {
                    mArrowView.setVisibility(View.VISIBLE);
                    if (category.isExpanded()) {
                        mArrowView.setImageResource(R.drawable.arrow_shang3_ic);  // 白色向上
                    } else {
                        mArrowView.setImageResource(R.drawable.arrow_xia3_ic);    // 灰色向下
                    }
                } else {
                    mArrowView.setVisibility(View.GONE);
                }
                
                mNameView.setTextColor(ContextCompat.getColor(getContext(),
                    category.isSelected() ? R.color.white : R.color.cb3));
            }
        }
    }

    /**
     * 分类点击回调接口
     */
    public interface OnCategoryClickListener {
        void onCategoryClick(int position);
    }
}

